package hopital.orm;

import hopital.sql.SqlObject;
import hopital.sql.SqlQuery;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Patient extends SqlObject {

    protected String _nom, _prenom, _adresse, _telephone;

    public Patient() {
    }

    public Patient(String _nom, String _prenom, String _adresse, String _telephone) {
        this._nom = _nom;
        this._prenom = _prenom;
        this._adresse = _adresse;
        this._telephone = _telephone;
    }

    public Patient(ResultSet _rs) throws SQLException {
        this(_rs.getString("c_nom"), _rs.getString("c_prenom"), _rs.getString("c_adresse"),
                _rs.getString("c_telephone"));
        this._id = _rs.getInt("c_id");
    }

    public String getNom() {
        return _nom;
    }

    public void setNom(String _nom) {
        this._nom = _nom;
    }

    public String getPrenom() {
        return _prenom;
    }

    public void setPrenom(String _prenom) {
        this._prenom = _prenom;
    }

    public String getAdresse() {
        return _adresse;
    }

    public void setAdresse(String _adresse) {
        this._adresse = _adresse;
    }

    public String getTelephone() {
        return _telephone;
    }

    public void setTelephone(String _telephone) {
        this._telephone = _telephone;
    }

    @Override
    public String toString() {
        return _nom + " " + _prenom;
    }

    @Override
    public SqlQuery insert() {
        return new SqlQuery("INSERT INTO t_patients (c_nom,c_prenom,c_adresse,c_telephone) VALUES(?,?,?,?);", _nom, _prenom, _adresse, _telephone);
    }

    @Override
    public SqlQuery update() {
        return new SqlQuery("UPDATE t_patients SET c_nom=?,c_prenom=?,c_adresse=?,c_telephone=? WHERE c_id=?;", _nom, _prenom, _adresse, _telephone, _id);
    }

    @Override
    public SqlQuery delete() {
        return new SqlQuery("DELETE FROM t_patients WHERE c_id=?;", _id);
    }

    @Override
    public SqlQuery select() {
        if (_id != -1) {
            return new SqlQuery("SELECT * FROM t_patients WHERE c_id=?;", _id);
        } else if (_nom != null && _prenom != null && _adresse != null && _telephone != null) {
            if (!_nom.equals("") && !_prenom.equals("") && !_adresse.equals("") && !_telephone.equals("")) {
                return new SqlQuery("SELECT * FROM t_patients WHERE c_nom LIKE ? OR c_prenom LIKE ? OR c_adresse LIKE ? OR c_telephone LIKE ?;", "%" + _nom + "%", "%" + _prenom + "%", "%" + _adresse + "%", "%" + _telephone + "%");
            } else if (!_nom.equals("")) {
                return new SqlQuery("SELECT * FROM t_patients WHERE c_nom LIKE ?;", "%" +_nom+ "%");
            } else if (!_prenom.equals("")) {
                return new SqlQuery("SELECT * FROM t_patients WHERE c_prenom LIKE ?;", "%" +_prenom+ "%");
            } else if (!_adresse.equals("")) {
                return new SqlQuery("SELECT * FROM t_patients WHERE c_adresse LIKE ?;", "%" +_adresse+ "%");
            } else if (!_telephone.equals("")) {
                return new SqlQuery("SELECT * FROM t_patients WHERE c_telephone LIKE ?;", "%" +_telephone+ "%");
            } else {
                return new SqlQuery("SELECT * FROM t_patients WHERE c_nom=? OR c_prenom=? OR c_adresse=? OR c_telephone=?;",  _nom ,  _prenom ,  _adresse ,  _telephone );
            }
        } else {
            return new SqlQuery("SELECT * FROM t_patients;");
        }
    }

    @Override
    public List<Object> getTable(ResultSet _rs) throws Exception {
        List<Object> list = new ArrayList<>();
        while (_rs.next()) {
            list.add(new Patient(_rs));
        }
        return list;
    }
}
